package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;

import core.AbstractAgent;
import core.Agent;
import core.Environment;
import core.Floor;
import core.Square;

public class MainJFrame extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	private JMenuBar jMenuBar;
	private JMenu menuFile;
	private JMenuItem closeMenuItem;
	private JMenuItem openMenuItem;

	public GridPanel gridPanel;
	public SettingsPanel settingsPanel;

	int size = 6;

	public Environment env;
	AbstractAgent agent;
	protected boolean stopped = true;
	private JMenuItem saveMenuItem;

	public static void main(String[] args) {
		new MainJFrame();
	}

	public MainJFrame() {
		super();
		initGUI();
	}

	private void initGUI() {
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());
		{
			jMenuBar = new JMenuBar();
			setJMenuBar(jMenuBar);
			{
				menuFile = new JMenu();
				jMenuBar.add(menuFile);
				menuFile.setText("File");
				{
					openMenuItem = new JMenuItem();
					openMenuItem.setText("Open");
					openMenuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							Serializzatore<Floor> serializzatore = new Serializzatore<Floor>();
							Floor floor = serializzatore.carica();
							if (floor != null) {
								env.floor.load(floor);
								remove(gridPanel);
								gridPanel = new GridPanel(env);
								getContentPane().add(gridPanel,
										BorderLayout.EAST);
								pack();
							}
							serializzatore.dispose();
						}
					});
					saveMenuItem = new JMenuItem();
					saveMenuItem.setText("Save");
					saveMenuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							Serializzatore<Floor> serializzatore = new Serializzatore<Floor>();
							serializzatore.salva(env.floor);
							serializzatore.dispose();
						}
					});
					closeMenuItem = new JMenuItem();
					closeMenuItem.setText("Close");
					closeMenuItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							System.exit(0);
						}
					});
					menuFile.add(openMenuItem);
					menuFile.add(saveMenuItem);
					menuFile.addSeparator();
					menuFile.add(closeMenuItem);
				}
			}
		}

		Random random = new Random();
		int room = Math.abs(random.nextInt()) % 4;
		int cell = Math.abs(random.nextInt()) % 8;
		agent = new Agent(room, cell, 100);
		env = new Environment(agent, Environment.Type.STATIC, 0);

		settingsPanel = new SettingsPanel(this);
		getContentPane().add(settingsPanel, BorderLayout.WEST);

		gridPanel = new GridPanel(env);
		getContentPane().add(gridPanel, BorderLayout.EAST);

		pack();
		this.setVisible(true);
	}

	public void mainLoop() {
		env.show();
		agent.actionList.clear();
		System.out.println("goal reached " + agent.goalReached());
		System.out.println("stopped " + stopped);
		System.out.println("time to Renata " + agent.timeToRenata);
		System.out.println("steps to change " + agent.maxSteps);
		while (!agent.goalReached() && !stopped && agent.timeToRenata > 0) {
			agent.timeToRenata--;
			env.update();
			env.show();
			gridPanel.update();
			settingsPanel.update();
			System.out.println("-------------------");
		}
		stopped = true;
		settingsPanel.update();
		System.out.println("Num actions: " + agent.actionList.size());
		agent.showActions();
		System.out.println("Performance: " + env.performanceMeasure());
		System.out.println("-- End --");
	}

	public void mainLoopOnes() {
		env.show();
		if (!agent.goalReached() && agent.timeToRenata > 0) {
			agent.timeToRenata--;
			env.update();
			env.show();
			gridPanel.update();
			settingsPanel.update();
			System.out.println("-------------------");
		}
		System.out.println("Num actions: " + agent.actionList.size());
		agent.showActions();
		System.out.println("Performance: " + env.performanceMeasure());
		System.out.println("-- End --");
	}

	public void newConfig(int newSize, Environment.Type envType,
			int timeToRenata) {
		System.out.println("Received size: " + newSize);
		agent = new Agent(0, 0, timeToRenata);
		env.floor = new Floor();
		env.floor.set(agent.x, agent.y, Square.Type.clean);
		env.agent = agent;
		env.type = envType;
		getContentPane().remove(gridPanel);
		gridPanel = new GridPanel(env);
		getContentPane().add(gridPanel, BorderLayout.EAST);
		gridPanel.update();
		pack();
	}
}